<?php

namespace Biz;
class Product extends \Base\BaseBiz
{
    /**
     * 新增商品
     * @param $data
     * @return mixed|null
     */
    public function insert($data){
        $product_orm = new \Orm\Product\ProductClass();
        $product_orm['name'] = $data['name'];
        $product_orm['description'] = $data['description'];
        $product_orm['type'] = $data['type'];
        $product_orm['total_num'] = $data['total_num'];
        $product_orm['residue_num'] = $data['residue_num'];
        $product_orm['portrait'] = $data['portrait'];
        $product_orm->insert();
        return $product_orm['id'];
    }

    /**
     * @param $data
     * 更新商品
     */
    public function update($data){
        $product_orm = new \Orm\Product\ProductClass();
        $product_orm->loadById($data['id']);
        $product_orm['name'] = $data['name'];
        $product_orm['description'] = $data['description'];
        $product_orm['type'] = $data['type'];
        $product_orm['total_num'] = $data['total_num'];
        $product_orm['residue_num'] = $data['residue_num'];
        $product_orm['portrait'] = $data['portrait'];
        $product_orm->update();
    }

    /**
     * @param 更新商品剩余库存
     */
    public function updateById($id){
        $product_orm = new \Orm\Product\ProductClass();
        $product_orm->loadById($id);
        if($product_orm['residue_num'] > 0){
            $product_orm['residue_num'] = $product_orm['residue_num'] - 1;
        }else{
            $product_orm['residue_num'] = 0;
        }
        $product_orm->update();
    }

    /**
     * @param $where
     * @param $now
     * @param $size
     * @return array
     * 获取商品列表
     */
    public function getList($where, $now, $size){
        $product_orm = new \Orm\Product\ProductList();
        if (!empty($where)) {
            $product_orm['where'] = $where;
        }
        $product_orm['limit'] = $size;
        $product_orm['offset'] = ($now - 1) * $size;
        $product_orm['order'] = 'id asc';
        $product_orm->load();
        $result = array();
        $result['lists'] = $product_orm['lists'];
        $result['total'] = $product_orm['total'];
        return $result;
    }

    /**
     * @param $id
     * 删除商品
     */
    public function delete($id){
        $product_orm = new \Orm\Product\ProductClass();
        $product_orm->delete($id);
    }

    /**
     * @param $type
     * @return mixed
     */
    public function getProductListByType($type){
        $product_orm = new \Orm\Product\ProductModel();
        return $product_orm->getProductListByType($type);
    }
}